package com.changgou.service.goods.controller;

import com.changgou.entity.PageResult;
import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.goods.pojo.Sku;
import com.changgou.service.goods.service.SkuService;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/sku")
public class SkuController {
    @Autowired
    private SkuService skuService;

    @GetMapping
    public Result<List<Sku>> findList() {
        List<Sku> skuList = skuService.findAll();
        return new Result<>(true, StatusCode.OK, "查询成功", skuList);
    }

    @GetMapping("/{id}")
    public Result<Sku> findById(@PathVariable("id") String id) {
        Sku sku = skuService.findById(id);
        return new Result<>(true, StatusCode.OK, "查询成功", sku);
    }

    @PostMapping
    public Result add(@RequestBody Sku sku) {
        skuService.add(sku);
        return new Result(true, StatusCode.OK, "添加成功");
    }

    @PutMapping("/{id}")
    public Result update(@RequestBody Sku sku,@PathVariable String id) {
        sku.setId(id);
        skuService.update(sku);
        return new Result(true, StatusCode.OK, "修改成功");
    }

    @DeleteMapping("/{id}")
    public Result delById(@PathVariable("id") String id) {
        skuService.delete(id);
        return new Result(true, StatusCode.OK, "删除成功");
    }

    @GetMapping("/search")
    public Result<List<Sku>> search(@RequestParam Map search) {
        List<Sku> list = skuService.findList(search);
        return new Result<>(true, StatusCode.OK, "查询成功", list);
    }

    @GetMapping("/search/{page}/{size}")
    public Result<List<Sku>> findPage(@PathVariable int page, @PathVariable int size) {
        Page<Sku> pageInfo = skuService.findPage(page, size);
        PageResult pageResult = new PageResult(pageInfo.getTotal(), pageInfo.getResult());
        return new Result<>(true, StatusCode.OK, "查询成功", pageResult);
    }

    @GetMapping("/searchPage/{page}/{size}")
    public Result<List<Sku>> findPage(@RequestParam Map searchMap,@PathVariable int page, @PathVariable int size) {
        Page pageInfo = skuService.findPage(searchMap, page, size);
        PageResult pageResult = new PageResult(pageInfo.getTotal(), pageInfo.getResult());
        return new Result<>(true, StatusCode.OK, "查询成功", pageResult);
    }

    @GetMapping("/spu/{spuId}")
    public List<Sku> findSkuListBySpuId(@PathVariable("spuId") String spuId) {
        HashMap<String, Object> hashMap = new HashMap();
        if (!"all".equals(spuId)) {
            hashMap.put("spuId", spuId);
        }
        hashMap.put("status", 1);
        return skuService.findList(hashMap);
    }

    @PostMapping("/decr/count")
    public Result decrCount(@RequestParam("username") String username){
        skuService.decrCount(username);
        return new Result(true,StatusCode.OK,"库存扣减成功");
    }
}
